package com.worldtolearn.user.dao;

import java.io.Serializable;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import org.springframework.stereotype.Repository;

import com.worldtolearn.user.entity.Person;
import com.worldtolearn.user.entity.User;

@Repository
public class UserDAO extends HibernateDaoSupport implements IUserDAO,
		Serializable {
	// public class UserDAO implements IUserDAO {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 
	 */
	@Autowired
	public void init(SessionFactory factory) {
		setSessionFactory(factory);
	}

	@Override
	public void create(Person user) {

		System.out
				.println("******************************************************************");
		System.out.println("UserDao - create(User user)");
		System.out.println("UserDao - create(User user) | " + user);
		System.out
				.println("******************************************************************");

		getHibernateTemplate().save(user);
		getHibernateTemplate().save(((User) user).getAddress());
	

	}

	@Override
	public User read(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void update(User user) {

		System.out
				.println("******************************************************************");
		System.out.println("UserDao - create(User user)");
		System.out.println("UserDao - create(User user) | " + user);
		System.out
				.println("******************************************************************");

		getHibernateTemplate().update(user.getAddress());
		getHibernateTemplate().update(user);

		// SingletonLogger.monLogger.info("The user has been successfully modified!");
	}

	@Override
	public void delete(User user) {

		System.out
				.println("******************************************************************");
		System.out.println("UserDao - delete(User user)");
		System.out.println("UserDao - delete(User user) | " + user);
		System.out
				.println("******************************************************************");

		getHibernateTemplate().delete(user.getAddress());
		getHibernateTemplate().delete(user);

		// SingletonLogger.monLogger.info("The user has been successfully deleted!");
	}

	@Override
	public User getUserByMail(String mail) {
		User user = new User();
		@SuppressWarnings("unchecked")
		List<User> l = getHibernateTemplate().find(
				"from Person where mail like '" + mail + "'");
		if (l.size() == 0) {
			System.out.println("Warning : no user having specified mail : "
					+ mail);
			user = null;
		} else if (l.size() >= 1) {
			user = l.get(0);
			if (l.size() > 1)
				System.out
						.println("Error : more than one user having a specified mail : "
								+ mail);
			System.out
			.println("Error : more than one user having a specified mail : "
					+ l.size());
			
			for (User user2 : l) {
				System.out.println("Error : more than one user having a specified mail  : user in l list : " +user2.getMail());
			}
		}
		return user;
	}

}
